package com.etc.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.etc.entity.Bus;
import com.etc.entity.BusInfo;
import com.etc.entity.User;
import com.etc.service.impl.BusServiceImpl;
import com.etc.service.impl.RoutePlanServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class BusController
 */
@WebServlet("/BusController")
public class BusController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BusController() {
		super();
		// TODO Auto-generated constructor stub
	}
	BusServiceImpl service = new BusServiceImpl();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取参数 参数名op的值如果等于query才做查询
		String op = request.getParameter("op");
		//提升:加入session的判断:从session中获取用户登录的信息,如果有,才能执行查询,添加等等的操作
		HttpSession session = request.getSession();
		Object object = session.getAttribute("backUser");
		if (object != null) {
			adminOp(op,request,response,session);
		}
		if("queryBus".equals(op)) {
			queryBus(request,response);
		}else if ("queryBusRoute".equals(op)) {
			queryBusRoute(request,response);
		}else if ("routeQuery".equals(op)) {
			routeQuery(request,response);
		}
	}

	private void routeQuery(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String start = request.getParameter("b_begin");
		String end = request.getParameter("b_end");
		HashMap<Integer, List> map = new RoutePlanServiceImpl().routePlan(start, end);
		Gson gson =new Gson();
		String json=gson.toJson(map);
		response.getWriter().print(json);
		response.getWriter().close();
		
	}

	private void queryBusRoute(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取数据
		int b_id = Integer.parseInt(request.getParameter("b_id"));
		List<String> list = service.getBusRoute(b_id);
		Gson gson =new Gson();
		String json=gson.toJson(list);
		response.getWriter().print(json);
		response.getWriter().close();
	}

	private void queryBus(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取数据
		String site = request.getParameter("s_name");
		List<BusInfo> busInfo = service.getBusIdAndEnd(site);
		Gson gson =new Gson();
		String json=gson.toJson(busInfo);
		response.getWriter().print(json);
		response.getWriter().close();
	}

	private void queryAll(HttpServletRequest request, HttpServletResponse response) {
		//设置查询默认值  用户未选择是默认第一页且查询所有
		int pageNo=1;
		int pageSize=10;
		//如果用户选择了数据 则修改数据否则按照 默认数据处理
		if(request.getParameter("pageNo")!=null) {
			pageNo=Integer.parseInt(request.getParameter("pageNo"));
		}
		if(request.getParameter("pageSize")!=null) {
			pageSize=Integer.parseInt(request.getParameter("pageSize"));
		}
		//调用业务层获取数据
		PageData pageData = service.getBusByLikeAndLimit(pageNo, pageSize);
		//使用ajax返回请求页面
		Gson gson =new Gson();
		String json=gson.toJson(pageData);
		try {
			response.getWriter().print(json);
			response.getWriter().close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void adminOp(String op, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException, ServletException {
		if ("updateBus".equals(op)) {
			updateBus(request,response);
		}else if ("busByPage_ajax".equals(op)) {
			queryAll(request,response);
		}else if ("deleteBus".equals(op)) {
			deleteBus(request,response);
		}else if ("add".equals(op)) {
			addBus(request,response);
		}
	}

	private void addBus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码格式
		response.setContentType("text/html;charset=utf-8");
		//接受参数
		String begin = request.getParameter("b_begin");
		String end = request.getParameter("b_end");
		int b_price= Integer.parseInt(request.getParameter("b_price"));
		int m_id= Integer.parseInt(request.getParameter("m_id"));
		int b_begin = service.getSiteId(begin);
		int b_end = service.getSiteId(end);
		Bus bus = new Bus(b_begin, b_end, b_price, m_id);
		boolean flag = service.insertBus(bus);
		if (flag) {
			response.getWriter().print("<script>location.href='back/bus-information.jsp'</script>");
		}
	}

	private void deleteBus(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//接受参数
		int b_id= Integer.parseInt(request.getParameter("b_id"));
		boolean flag = service.delBus(b_id);
		Gson gson=new Gson();
		String jsonFlag=gson.toJson(flag);
		response.getWriter().print(jsonFlag);
		response.getWriter().close();
	}

	private void updateBus(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//接受参数
		String jsonString= request.getParameter("busInfo");
		//获取java对象
		Gson gson =new Gson();
		BusInfo busInfo = gson.fromJson(jsonString,BusInfo.class);
		//调用修改方法
		boolean flag = service.updateBus(busInfo);
		//返回数据
		String jsonFlag=gson.toJson(flag);
		response.getWriter().print(jsonFlag);
		response.getWriter().close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
